<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Manage all self post
 *
 * @author tuancasi
 */
class My_postController extends Controller {

    public function init() {
	if (!user()->isGuest) {
	    
	} else {
	    $this->redirect(SITE_NAME_URL . 'user/login');
	}
    }

    public function actionIndex() {
	//get latest ad
	$criteria = new CDbCriteria();
	$criteria->with = 'categories';
	$criteria->addCondition('t.user_id=' . user()->id);
	//order by last update
	$criteria->order = 't.updated DESC';

	$dataProvider = new CActiveDataProvider('Ad', array(
	    'criteria' => $criteria
	));
	$this->render('index', array(
	    'dataProvider' => $dataProvider
	));
    }

    public function actionEdit($id) {

	$model = $this->loadModel($id);

	//check owner post
	if ($model->user_id == user()->id) {
	    // Uncomment the following line if AJAX validation is needed
	    //$this->performAjaxValidation($model);

	    if (r()->isPostRequest) {
		$result = false;
		switch ($model->type) {
		    case Ad::TYPE_EQUIPMENT:
			$result = $this->_registerEquipment($model);
			break;
		    case Ad::TYPE_RESUME :
			$model->resume_builder = serialize($_POST['ResumeBuilder']);
			$result = $this->_registerResume($model);
			break;
		    case Ad::TYPE_JOB :
			$result = $this->_registerJob($model);
			break;
		    case Ad::TYPE_SUPPLY :
			$result = $this->_registerSupply($model);
			break;
//		    case Ad::TYPE_COMPANY_SOURCE :
//			$eblast = Eblast::model()->find('ad_id=' . $model->ad_id);
//			$result = $this->_registerCompanySource($model, $eblast);
//			break;
		    case Ad::TYPE_VENDOR :
			$eblast = Eblast::model()->find('ad_id=' . $model->ad_id);
			$result = $this->_registerCompanySource($model, $eblast, 'Vendor');
			break;
		    case Ad::TYPE_TECHNICAL :
			$eblast = Eblast::model()->find('ad_id=' . $model->ad_id);
			$result = $this->_registerCompanySource($model, $eblast, 'Technical');
			break;
		    case Ad::TYPE_ADVERTISE:
			$this->_registerAdvertise($model);
			break;
		    default :
			$result = $this->_registerEquipment($model);
			break;
		}

		if ($result) {
		    //redirect to update
//		$this->redirect(array('update', 'id' => $result));
		}
	    }
	    $aCatAds = array();
	    if ($model->categories) {
		foreach ($model->categories as $cat) {
		    $aCatAds[] = $cat->category_id;
		}
	    }
	    $this->render('edit', array(
		'model' => $model,
		'aCatAds' => $aCatAds
	    ));
	} else {
	    $this->render('edit', array('notOwn' => 1));
	}
    }

    /**
     *
     * @param Company $model
     * @return mixed false if save false, total price if save successfully
     */
    private function _registerCompanySource($model, $eblast, $typePost) {

	$image = $model->image;
	$advertisement = $model->advertisement;
	$adsEblast = $eblast->advertisement;
	unset($_POST[$typePost]['featured']);

	$model->attributes = $_POST[$typePost];
	//$model->title = 'Company source';
	$model->image = $image;
	$model->advertisement = $advertisement;


	//save image
	if (($fileHandler = CUploadedFile::getInstance($model, 'image'))) {
	    //delete old file
	    if ($model->image) {
		deleteFile(UPLOAD_PATH . $model->image);
	    }
	    $model->image = UploadHelper::saveImage($fileHandler);
	}
	//save advertisement image
	if (($fileHandler = CUploadedFile::getInstance($model, 'advertisement'))) {
	    //delete old file
	    if ($model->advertisement) {
		deleteFile(UPLOAD_PATH . 'feature/' . $model->image);
	    }
	    $model->advertisement = UploadHelper::saveImage($fileHandler, 'feature');
	}


	if ($model->validate()) {

	    $flag = true;

	    //eblast validate
	    if (!$eblast) {
		$eblast = new Eblast();
	    }
	    if (isset($_POST['eblast'])) {
		$eblast->attributes = $_POST['Eblast'];
		$eblast->advertisement = $adsEblast;
		$eblast->start_date = date("Y-m-d", strtotime($_POST['Eblast']['start_date']));
		//save eblast
		if (($fileHandler = CUploadedFile::getInstance($eblast, 'advertisement'))) {
		    if ($eblast->advertisement) {
			deleteFile(UPLOAD_PATH . 'eblast/' . $eblast->advertisement);
		    }
		    $eblast->advertisement = UploadHelper::saveDocument($fileHandler, 'eblast');
		}
		if (!$eblast->validate()) {
		    $flag = false;
		} else {
		    $eblast->save();
		}
	    }

	    if ($flag) {
		if ($model->save()) {
		    return $model->ad_id;
		}
	    }
	}

	return false;
    }

    /**
     *
     * @param Equipment $model
     * @return mixed false if save false, total price if save successfully
     */
    private function _registerEquipment($model) {
	$image = $model->image;
	$model->attributes = $_POST['Equipment'];
	$model->image = $image;
	//save image
	if (($fileHandler = CUploadedFile::getInstance($model, 'image'))) {
	    if ($model->image) {
		deleteFile(UPLOAD_PATH . $model->image);
	    }
	    $model->image = UploadHelper::saveImage($fileHandler);
	}

	if ($model->validate()) {
	    if ($model->save()) {
		return $model->ad_id;
	    }
	}

	return false;
    }

    /**
     *
     * @param Supply $model
     * @return mixed false if save false, total price if save successfully
     */
    private function _registerSupply($model) {
	$image = $model->image;
	$model->attributes = $_POST['Supply'];
	$model->image = $image;
	//save image
	if (($fileHandler = CUploadedFile::getInstance($model, 'image'))) {
	    if ($model->image) {
		deleteFile(UPLOAD_PATH . $model->image);
	    }
	    $model->image = UploadHelper::saveImage($fileHandler);
	}

	if ($model->validate()) {
	    if ($model->save()) {
		return $model->ad_id;
	    }
	}

	return false;
    }

    /**
     *
     * @param Job $model
     * @return mixed false if save false, total price if save successfully
     */
    private function _registerJob($model) {
	$model->attributes = $_POST['Job'];
	$model->title = 'Job';
	if ($model->validate()) {
	    if ($model->save()) {
		return $model->ad_id;
	    }
	}

	return false;
    }

    /**
     *
     * @param Job $model
     * @return mixed false if save false, total price if save successfully
     */
    private function _registerResume($model) {
	$resume = $model->resume;
	$model->attributes = $_POST['Resume'];
	$model->title = 'Resume';
	$model->resume = $resume;
	if ($fileHandle = CUploadedFile::getInstance($model, 'resume')) {
	    if ($model->resume) {
		deleteFile(UPLOAD_PATH . 'documents/' . $model->resume);
	    }
	    $model->resume = UploadHelper::saveDocument($fileHandle);
	}
	if ($model->validate()) {
	    if ($model->save()) {
		return $model->ad_id;
	    }
	}

	return false;
    }

    /*
     * 
     */

    private function _registerAdvertise($model) {
	$image = $model->image;
	$model->attributes = $_POST['Advertise'];
	$model->image = $image;
	//save image
	if (($fileHandler = CUploadedFile::getInstance($model, 'image'))) {
	    if ($model->image) {
		deleteFile(UPLOAD_PATH . $model->image);
	    }
	    $model->image = UploadHelper::saveImage($fileHandler);
	}
	if ($model->validate()) {
	    if ($model->save()) {
		return $model->ad_id;
	    }
	}

	return false;
    }

    /**
     * delete post
     */
    public function actionDelete_post() {
	if (isset($_POST['adId'])) {
	    $model = $this->loadModel($_POST['adId']);
	    $model->delete();
	}
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id) {
	$model = Ad::model()->with('categories')->findByPk($id);
	if ($model === null)
	    throw new CHttpException(404, 'The requested page does not exist.');
	return $model;
    }

}

